package com.yenlien.traceyun.business.controller;

import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.business.dao.SubpackageDao;
import com.yenlien.traceyun.business.entity.Logistics;
import com.yenlien.traceyun.business.entity.StorageManage;
import com.yenlien.traceyun.business.entity.Subpackage;
import com.yenlien.traceyun.business.interceptor.UserAuthUtil;
import com.yenlien.traceyun.business.service.StorageManageService;
import com.yenlien.traceyun.webase.controller.BaseController;
import com.yenlien.traceyun.webase.exception.BusinessException;
import com.yenlien.traceyun.webase.utils.CheckParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by LiuSha on 2020/7/29.
 */
@RestController
@RequestMapping("/storage/manage")
public class StorageManageController extends BaseController {
    @Autowired
    private StorageManageService storageManageService;


    @Autowired
    private SubpackageDao subpackageDao;

    /**
     * 入库
     *
     * @param params
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Object save(@RequestBody JSONObject params) {
        List<StorageManage> storageManage = params.getJSONArray("storages").toJavaList(StorageManage.class);
        Subpackage subpackage = null;
        List<String> codes = null;
        if (params.containsKey("sub")) {
            subpackage = params.getJSONObject("sub").toJavaObject(Subpackage.class);
        }
        if (params.containsKey("codes")) {
            codes = params.getJSONArray("codes").toJavaList(String.class);
        }
        storageManageService.save(storageManage, subpackage, codes);
        return success("success");
    }


    @RequestMapping(value = "/findByList", method = RequestMethod.POST)
    public Object findByList(@RequestBody StorageManage storageManage) {
        if (StringUtils.isEmpty(storageManage.getCompanyId())) {
            storageManage.setCompanyId(UserAuthUtil.getLoginUser().getCompanyId());
        }
        Page<StorageManage> storageManages = storageManageService.findByList(storageManage);
        return buildPage(storageManages);
    }

    //调整库存
    @RequestMapping(value = "/adjustment", method = RequestMethod.POST)
    public Object adjustment(@RequestBody JSONObject jsonObject) {
        CheckParams.checkJson(jsonObject, new String[]{"storageId", "actualNum"});
        storageManageService.adjustment(jsonObject);
        return success("success");
    }

    /**
     * 出库
     *
     * @param params
     * @return
     */
    @RequestMapping(value = "/out", method = RequestMethod.POST)
    public Object out(@RequestBody JSONObject params) {
        //订单部分信息
        Logistics logistics = params.getJSONObject("logistics").toJavaObject(Logistics.class);
        storageManageService.out(logistics, params.getJSONArray("detailCodes"));
        return null;
    }


    /**
     * 明细
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET)
    public Object findById(@PathVariable("id") String id) {
        return success(storageManageService.findById(id));
    }


    /**
     * 入库后根据storageManageId下载码
     *
     * @param request
     */
    @GetMapping(value = "/down/codes")
    public void inDownloadCodes(HttpServletRequest request, HttpServletResponse response) throws IOException {
        CheckParams.checkRequest(request, new String[]{"type"});
        String type = request.getParameter("type");
        String storageId = request.getParameter("storageId");
        String storageDetailId = request.getParameter("storageDetailId");
        if (!(type.equals("out") || type.equals("in"))) {
            throw new BusinessException("500", "type参数只能是in或者out");
        }
        if (type.equals("in") && StringUtils.isEmpty(storageId)) {
            throw new BusinessException("500", "导出入库，storageId不能为空");
        }
        if (type.equals("out") && StringUtils.isEmpty(storageDetailId)) {
            throw new BusinessException("500", "导出出库，storageDetailId不能为空");
        }
        response.setHeader("content-type", "application/octet-stream");
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment;filename=code.txt");
        storageManageService.downCodes(type, storageId, storageDetailId, response.getOutputStream());
    }

    /**
     * 出库的时候根据检查追溯码是否合理正确
     *
     * @param storageId
     * @param code
     * @return
     */
    @GetMapping(value = "/out/checkcode/{storageId}/{code}")
    public Object checkCodeRight(@PathVariable("storageId") String storageId, @PathVariable("code") String code) {
        boolean right = storageManageService.checkCodeRight(storageId, code);
        return success(right);
    }


}
